<?php
class DistritosController extends AppController {

	var $name = 'Distritos';

	function index() {
		$this->Distrito->recursive = 0;
		$this->set('distritos', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid distrito', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('distrito', $this->Distrito->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Distrito->create();
			if ($this->Distrito->save($this->data)) {
				$this->Session->setFlash(__('The distrito has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The distrito could not be saved. Please, try again.', true));
			}
		}
		$provincias = $this->Distrito->Provincium->find('list');
		$paises = $this->Distrito->Paise->find('list');
		$empresas = $this->Distrito->Empresa->find('list');
		$this->set(compact('provincias', 'paises', 'empresas'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid distrito', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Distrito->save($this->data)) {
				$this->Session->setFlash(__('The distrito has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The distrito could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Distrito->read(null, $id);
		}
		$provincias = $this->Distrito->Provincium->find('list');
		$paises = $this->Distrito->Paise->find('list');
		$empresas = $this->Distrito->Empresa->find('list');
		$this->set(compact('provincias', 'paises', 'empresas'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for distrito', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Distrito->delete($id)) {
			$this->Session->setFlash(__('Distrito deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Distrito was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
